from django.shortcuts import render
from rest_framework.views import APIView
from django.core.paginator import Paginator
from rest_framework.response import Response
from django.contrib.auth.hashers import make_password,check_password
from . import models
from .serializer import *
from django.conf import settings
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
# Create your views here.

user_token=Serializer(settings.SECRET_KEY)

class Register(APIView):
    def post(self,request):
        username=request.data.get('username')
        password=request.data.get('password')
        password1=request.data.get('password1')
        user=models.User.objects.filter(username=username).first()
        if user:
            return Response({'msg':'用户已存在','code':1001})
        if password==password1:
            data={'username':username,
                  'password':make_password(password),
                  'img':request.data.get('img'),
                  'name':request.data.get("name"),
                  'sign':request.data.get('sign')}
            obj=UserModelSerialzier(data=data)
            if obj.is_valid():
                obj.save()
                return Response({'msg':'注册成功','code':1000})
            print(obj.errors)
            return Response({'msg': '注册失败', 'code': 1001})
        else:
            return Response({'msg': '密码错误', 'code': 1002})

    def get(self,request):
        user=models.User.objects.all()
        user_serializer=UserModelSerialzier(user,many=True)
        return Response(user_serializer.data)


class Login(APIView):
    def post(self,request):
        username=request.data.get('username')
        password=request.data.get('password')
        user=models.User.objects.filter(username=username).first()
        if user:
            if check_password(password,user.password):
                user_info={'user_id':user.id}
                token=user_token.dumps(user_info).decode()
                return Response({'msg': '登录成功', 'code': 1000, 'token': token})
            else:
                return Response({'msg': '用户名或密码错误', 'code': 1001})
        else:
            return Response({'msg': '登录失败', 'code': 1002})


class AddArticle(APIView):
    def post(self,request):
        obj=ArticleModelSerializer(data=request.data)
        if obj.is_valid():
            obj.save()
            return Response({'msg': '添加成功', 'code': 1000})
        print(obj.errors)
        return Response({'msg': '添加失败', 'code': 1001})

class ShowPaginator(APIView):
    def get(self,request,uid):
        article=models.Travels.objects.all()
        paginotor=Paginator(article,5)
        paged=paginotor.page(uid)
        article_serializer=ArticleModelSerializer(paged,many=True)
        return Response({'page_num':paginotor.num_pages,
                         'page_list':[i for i in paginotor.page_range],
                         'data':article_serializer.data})

class ShowArtilce(APIView):
    def get(self,request):
        title=request.GET.get('title')
        obj=models.Travels.objects.get(title=title)
        obj.view_count +=1
        obj.save()
        article_serializer=ArticleModelSerializer(obj)
        return Response(article_serializer.data)

class ShowUser(APIView):
    def get(self,request):
        token=request.GET.get('token')
        user_info=user_token.loads(token)
        user_id=user_info.get('user_id')
        user_obj=models.User.objects.get(pk=user_id)
        user_serializer=UserModelSerialzier(user_obj)
        return Response(user_serializer.data)
